<template>
  <div class="app-container">
    <!--搜索区-->
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <div style="margin-top: 15px">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">
            <el-form-item label="区域名称：">
              <el-input v-model="queryParams.name" class="input-width" placeholder="请输入区域名称" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery()" size="small">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery()" size="small">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>

    <!--操作按钮区-->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
<!--      <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      <el-button style="margin-left: 20px" type="primary" plain icon="el-icon-circle-plus" size="mini" @click="handleAdd" v-hasPermi="['system:area:create']">新增</el-button>
    </el-card>

    <!--数据列表区-->
    <div class="table-container">
      <el-table v-loading="loading" :data="areaList"
                style="width: 100%;"
                row-key="areaId"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                border>
<!--        <el-table-column-->
<!--            type="selection"-->
<!--            width="55">-->
<!--        </el-table-column>-->
        <el-table-column prop="name" label="区域名称"  width="160" />
        <el-table-column  label="区域类型"  align="center" >
          <template slot-scope="scope">{{ scope.row.type | setTypeName }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:area:edit']"
            >修改</el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:area:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加或修改省市区表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form :model="form" ref="form" :rules="rules" label-width="100px" size="small">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级菜单">
              <treeselect
                  v-model="form.parentId"
                  :options="menuOptions"
                  :normalizer="normalizer"
                  :show-count="true"
                  placeholder="选择上级菜单"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="区域类型" prop="type">
              <el-radio-group v-model="form.type">
                <el-radio :label=0>省</el-radio>
                <el-radio :label=1>市</el-radio>
                <el-radio :label=2>区</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24" >
            <el-form-item label="区域名称">
              <el-input v-model="form.name" placeholder="请输入区域名称" maxlength="255" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/sys/area";
import Treeselect from '@riophae/vue-treeselect'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {exportBlob} from "@/api/common"
import {downloadXls} from "@/utils/util"

export default {
  name: "Area",
  filters: {
    setTypeName(val) {
      let text = ''
      switch (val) {
        case 0:
          text = '省'
          break
        case 1:
          text = '市'
          break
        case 2:
          text = '区'
          break
        default :
          text = ''
      }
      return text
    }
  },
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 省市区表表格数据
      areaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        name: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      menuOptions: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询省市区表列表 */
    getList() {
      this.loading = true;
      listArea(this.queryParams).then(response => {
        this.areaList = this.handleTree(response.data, "areaId");
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        areaId: null,
        parentId: null,
        name: null,
        type: null,
        updatedTime: null,
        updatedBy: null,
        createdTime: null,
        createdBy: null,
        deleted: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.name = ''
      this.handleQuery();
    },
    // handleSelectionChange(val) {
    //   console.log(val);
    // },
    /** 转换行政区划数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.areaId,
        label: node.name,
        children: node.children
      };
    },
    /** 查询行政区划下拉树结构 */
    getTreeselect() {
      listArea().then(response => {
        this.menuOptions = [];
        const menu = { areaId: 0, name: '主类目', children: [] };
        menu.children = this.handleTree(response.data, "areaId");
        this.menuOptions.push(menu);
      });
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = "添加行政区划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const areaId = row.areaId || this.ids
      getArea(areaId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改行政区划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.areaId != null) {
            updateArea(this.form.areaId,this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArea(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const areaIds = row.areaId || this.ids;
      this.$modal.confirm('是否确认删除行政区划编号为"' + areaIds + '"的数据项？').then(function() {
        return delArea(areaIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      exportBlob(`system/area/export`).then(res => {
        downloadXls(res.data, "用户数据模板.xlsx");
      })
    },

  }
};
</script>
